#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
小红书发送验证码请求模拟脚本
完全按照开发者工具中的请求信息构建
"""

import requests
import json
from urllib.parse import urlencode


def send_xiaohongshu_code():
    """
    模拟小红书发送验证码的请求
    """
    
    # 请求URL和参数
    base_url = "https://edith.xiaohongshu.com/api/sns/web/v2/login/send_code"
    params = {
        "phone": "17700000001",
        "zone": "86",
        "type": "login"
    }
    
    # 完整的请求头，完全按照开发者工具中的信息
    headers = {
        ":authority": "edith.xiaohongshu.com",
        ":method": "GET",
        ":path": "/api/sns/web/v2/login/send_code?phone=16521767748&zone=86&type=login",
        ":scheme": "https",
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "zh-CN,zh;q=0.9",
        "cookie": "acw_tc=0ad5868d17521596271866698e470d4a0b3afae0816d3640feca36925b0c47",
        "origin": "https://www.xiaohongshu.com",
        "priority": "u=1, i",
        "referer": "https://www.xiaohongshu.com/",
        "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": '"Windows"',
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
        "x-b3-traceid": "d7a651fafca51896",
        "x-s": "XYS_2UQhPsHCH0c1PjhlHjIj2erjwjQhyoPTqBPT49pjHjIj2eHjwjQgynEDJ74AHjIj2ePjwjQTJdPIP/ZlgMHApe+TajR/ySHM8d4E+SmiaLpP4rPMnDR+nLEjPg8TPg4NpBMayF8ccD+iGpbf+rT0aLi3wnba8rGMJ0Yk20m7aopozpbIcf8BzDpzPDEI8nkb2/rA/nzr4MS6GFTj//S8+9+M/ePjNsQh+sHCHjQR",
        "x-s-common": "2UQAPsHC+aIjq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7pNa/PMqAbV+9pn8f4Ap9ktqM+n4FEQyM8eaL++zjRc47kQPFz8anV68gYYLM4t+FRA8db7tUHVHdWEH0iTP/PE+ALhP/c7+aIj2erIH0iINsQhP/rjwjQ1J7QTGnIjKc==",
        "x-t": "1752159587086",
        "x-xray-traceid": "cbfa6cc581df72071789956c6af760b7"
    }
    
    # 由于HTTP/2的伪头部字段在requests中不被支持，我们需要移除它们
    # 只保留标准的HTTP头部
    standard_headers = {
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "zh-CN,zh;q=0.9",
        "cookie": "acw_tc=0ad5868d17521596271866698e470d4a0b3afae0816d3640feca36925b0c47",
        "origin": "https://www.xiaohongshu.com",
        "referer": "https://www.xiaohongshu.com/",
        "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": '"Windows"',
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
        "x-b3-traceid": "d7a651fafca51896",
        "x-s": "XYS_2UQhPsHCH0c1PjhlHjIj2erjwjQhyoPTqBPT49pjHjIj2eHjwjQgynEDJ74AHjIj2ePjwjQTJdPIP/ZlgMHApe+TajR/ySHM8d4E+SmiaLpP4rPMnDR+nLEjPg8TPg4NpBMayF8ccD+iGpbf+rT0aLi3wnba8rGMJ0Yk20m7aopozpbIcf8BzDpzPDEI8nkb2/rA/nzr4MS6GFTj//S8+9+M/ePjNsQh+sHCHjQR",
        "x-s-common": "2UQAPsHC+aIjqArjwjHjNsQhPsHCH0rjNsQhPaHCH0c1PjhlHjIj2eHjwjQgynEDJ74AHjIj2ePjwjQhyoPTqBPT49pjHjIj2ecjwjHFN0qUN0ZjNsQh+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sQhP/rjwjQ1J7QTGnIjKc==",
        "x-t": "1752159587086",
        "x-xray-traceid": "cbfa6cc581df72071789956c6af760b7"
    }
    
    print("开始发送小红书验证码请求...")
    print(f"请求URL: {base_url}")
    print(f"请求参数: {params}")
    print("=" * 50)
    
    # 发送GET请求
    response = requests.get(
        base_url,
        params=params,
        headers=standard_headers,
        timeout=30
    )
    
    print(f"响应状态码: {response.status_code}")
    print(f"响应头: {dict(response.headers)}")
    print("=" * 50)
    
    # 处理响应
    if response.status_code == 200:
        try:
            response_data = response.json()
            print("响应内容 (JSON):")
            print(json.dumps(response_data, indent=2, ensure_ascii=False))
        except json.JSONDecodeError:
            print("响应内容 (文本):")
            print(response.text)
    else:
        print(f"请求失败，状态码: {response.status_code}")
        print(f"错误信息: {response.text}")
    
    return response


def main():
    """
    主函数
    """
    print("小红书发送验证码请求模拟器")
    print("=" * 50)
    
    # 执行请求
    response = send_xiaohongshu_code()
    
    print("\n请求完成!")
    print(f"最终状态码: {response.status_code}")


if __name__ == "__main__":
    main() 